package com.yuanrui.subject.domain.handler.subject;

import com.yuanrui.subject.common.enums.IsDeletedFlagEnum;
import com.yuanrui.subject.common.enums.SubjectInfoTypeEnum;
import com.yuanrui.subject.domain.convert.SubjectMultipleConvert;
import com.yuanrui.subject.domain.entity.SubjectAnswerBO;
import com.yuanrui.subject.domain.entity.SubjectInfoBO;
import com.yuanrui.subject.domain.entity.SubjectOptionBO;
import com.yuanrui.subject.infra.basic.entity.SubjectMultiple;
import com.yuanrui.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目策略类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getSubjectType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //多选题目的添加逻辑
        if (subjectInfoBO.getOptionList().isEmpty()){
            return;
        }
        List<SubjectMultiple> subjectMultiples=new LinkedList<>();
        //多选题目的添加逻辑
        subjectInfoBO.getOptionList().forEach(option -> {
            //多选题目的添加逻辑
            SubjectMultiple subjectMultiple = SubjectMultipleConvert.INSTANCE.convertToBoEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsCorrect(option.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectMultiple.setOptionContent(option.getOptionContent());
            //保存多选题目信息
            subjectMultiples.add(subjectMultiple);
        });
        subjectMultipleService.insertBatch(subjectMultiples);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMultiple> subjectMultiples = subjectMultipleService.queryBySubjectId(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOS = SubjectMultipleConvert.INSTANCE.convertToAnswerBo(subjectMultiples);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }

    @Override
    public SubjectInfoBO queryAll(Long subjectId) {
        return null;
    }
}
